package cn.itsource.controller.manager;

import cn.itsource.pojo.dto.CancelOrderDto;
import cn.itsource.pojo.dto.ConfirmFeesDto;
import cn.itsource.pojo.dto.OrderDto;
import cn.itsource.service.IOrderService;
import cn.itsource.pojo.domain.Order;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.itsource.pojo.query.PageQueryWrapper;
import cn.itsource.result.JSONResult;
import cn.itsource.result.PageResult;

@Tag(name = "订单表",description = "订单表")
@RestController
@RequestMapping("/manager/order")
public class OrderController{

    @Autowired
    public IOrderService orderService;

    /**
     * 计算订单费用
     * @param confirmFeesDto
     * @return
     */
    @PostMapping("/confirm")
    public JSONResult confirmFees(@RequestBody @Valid ConfirmFeesDto confirmFeesDto){
        return orderService.confirmFees(confirmFeesDto);
    }

    /**
     * 开始代驾
     * @param orderNo
     * @return
     */
    @GetMapping("/startDrivingHandle/{orderNo}")
    public JSONResult startDrivingHandle(@PathVariable("orderNo")String orderNo){
        return orderService.startDrivingHandle(orderNo);
    }

    /**
     * 到达代驾点
     * @param orderNo
     * @return
     */
    @GetMapping("/arriveStartPlaceHandle/{orderNo}")
    public JSONResult arriveStartPlaceHandle(@PathVariable("orderNo")String orderNo){
        return orderService.arriveStartPlaceHandle(orderNo);
    }

    /**
     * 结束代驾
     * @param orderNo
     * @return
     */
    @GetMapping("/endDrivingHandle/{orderNo}")
    public JSONResult endDrivingHandle(@PathVariable("orderNo")String orderNo){
        return orderService.endDrivingHandle(orderNo);
    }

    // 拿到司乘同显的位置数据
    @GetMapping("/location/{orderNo}")
    public JSONResult location(@PathVariable("orderNo")String orderNo){
        return JSONResult.success(orderService.getOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNo,orderNo)));
    }

    /**
     *  乘客是否已被接单 ，自动倒计时就不能改变订单状态了
     * @return
     */
    @GetMapping("/receivedAnOrder/{orderNo}")
    public JSONResult receivedAnOrder(@PathVariable("orderNo") String orderNo){
        return orderService.receivedAnOrder(orderNo);
    }

    /**
     * 判断司机是否已经接单
     * @return
     */
    @GetMapping("/driverRunningOrder")
    public JSONResult driverRunningOrder(){
        return orderService.driverRunningOrder();
    }

    /**
     * 判断当前乘客是否有订单运行
     * @return
     */
    @GetMapping("/checkRunningOrder")
    public JSONResult checkRunningOrder(){
        return orderService.checkRunningOrder();
    }

    /**
     * 司机抢单
     * @param orderNo
     * @return
     */
    @GetMapping("/grabOrder/{orderNo}")
    public JSONResult grabOrder(@PathVariable("orderNo" ) String orderNo){
        return orderService.grabOrder(orderNo);
    }

    /**
     * 乘客/司机 取消订单
     * @param cancelOrderDto
     * @return
     */
    @PostMapping("/cancelOrder")
    public JSONResult cancelOrder(@RequestBody CancelOrderDto cancelOrderDto){
        JSONResult jsonResult = orderService.cancelOrder(cancelOrderDto);
        return jsonResult;
    }

    /**
     * 司机拉去乘客订单的信息
     * @return
     */
    @GetMapping("/pullCustomerOrder")
    public JSONResult pullCustomerOrder(){
        return orderService.pullCustomerOrder();
    }

    @Operation( summary= "保存Order",description = "基础对象保存接口")
    @Parameter(name = "order",description = "保存的对象",required = true)
    @PostMapping("/addOrder")
    public JSONResult save(@RequestBody @Valid OrderDto orderDto){
        return orderService.save(orderDto);
    }

    @Operation( summary= "修改Order",description = "基础对象修改接口")
    @Parameter(name = "order",description = "修改的对象",required = true)
    @PutMapping
    public JSONResult update(@RequestBody  @Valid Order order){
        return JSONResult.success(orderService.updateById(order));
    }

    //删除对象
    @Operation( summary= "删除Order",description = "基础对象删除接口，采用状态删除")
    @Parameter(name = "id",description = "删除的对象ID",required = true)
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        return JSONResult.success(orderService.removeById(id));
    }

    //获取对象
    @Operation( summary= "获取Order",description = "基础对象获取接口")
    @Parameter(name = "id",description = "查询的对象ID",required = true)
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(orderService.getById(id));
    }

    //获取列表:PageQueryWrapper作为通用的查询对象
    @Operation( summary= "查询Order列表",description = "基础对象列表查询，不带分页")
    @Parameter(name = "query",description = "查询条件",required = true)
    @PostMapping(value = "/list")
    public JSONResult list(@RequestBody PageQueryWrapper<Order> query){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        return JSONResult.success(orderService.list(wrapper));
    }

    //分页查询
    @Operation( summary= "查询Order分页列表",description = "基础对象列表查询，带分页")
    @Parameter(name = "query",description = "查询条件，需要指定分页条件",required = true)
    @PostMapping(value = "/pagelist")
    public JSONResult page(@RequestBody PageQueryWrapper<Order> query){
        //分页查询
        Page<Order> page = new Page<Order>(query.getPage(),query.getRows());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        //分页查询
        page = orderService.page(page,wrapper);
        //返回结果
        return JSONResult.success(new PageResult<Order>(page.getTotal(),page.getRecords()));
    }

}
